function retval = FunctionCode(item_name, func_code)
%FUNCTIONCODES Perform NASTRAN functions on DATABLK values
%   RETVAL = FUNCTIONCODES(ITEM_NAME, FUNC_CODE) performs function
%   FUNC_CODE on the value referenced by ITEM_NAME.
%
%   Used by: READBLOCK*.
%
%   See also:
%   - MD Nastran 2006 DMAP Programmer's Guide, page 840
%   - NX Nastran DMAP Programmer's Guide 2007, page 2-5

%   $Revision: 1.1.0 $  $Date: 2022/09/08 $

switch func_code
    case 1
        % guides differ
        switch floor(item_name/1000)
            case {2 3 4 6}
                retval = 2;
            otherwise
                retval = 1;
        end
    case 2
        retval = mod(item_name, 100);
    case 3
        % guides differ
        retval = mod(item_name, 1000);
    case 4
        retval = floor(item_name/10);
    case 5
        retval = mod(item_name, 10);
    case 6
        if bitand(item_name, 8) ~= 0
            retval = 0;
        else
            retval = 1;
        end
    case 7
        % guides differ
        switch floor(item_name/1000)
            case {0, 2}
                retval = 0;
            case {1, 3}
                retval = 1;
            otherwise
                retval = 2;
        end
    case 8
        retval = bitand(131071, item_name);
    otherwise
        if func_code > 65535
            retval = bitand(item_name, bitand(func_code, 65535));
        else
            error('functionCodes: Wrong func_code');
        end
end

